package com.h2n.framework.dao.classes;

import com.h2n.framework.dao.IMemberDAO;
import com.h2n.framework.dao.ConstantsDAO;
import com.h2n.framework.dto.MemberDTO;
import com.h2n.utils.Convert;
import com.h2n.utils.EDatetimeFormat;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

/**
 * Created by IntelliJ IDEA.
 * User: NhatNV
 * Date: Jul 30, 2009
 * Time: 12:41:07 AM
 * To change this template use File | Settings | File Templates.
 */
public class MemberDAO implements IMemberDAO {
  public MemberDAO(){
  }

  private JdbcTemplate jdbcTemplate;
  public JdbcTemplate getJdbcTemplate(){
    return jdbcTemplate;
  }
  public void setJdbcTemplate(JdbcTemplate _jdbcTemplate){
    jdbcTemplate = _jdbcTemplate;
  }

  @Override
  public long insert(MemberDTO _memberDTO) throws Exception {
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName(MemberDTO.INSERT_PROCEDURE);
    MapSqlParameterSource inParams = new MapSqlParameterSource(_memberDTO.getMap());
    Map outParams = simpleJdbcCall.execute(inParams);

    return Long.parseLong(outParams.get(MemberDTO.Parameters.p_id).toString());
  }

  @Override
  public void update(MemberDTO _memberDTO) throws Exception {
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName(MemberDTO.UPDATE_PROCEDURE);
    MapSqlParameterSource inParams = new MapSqlParameterSource(_memberDTO.getMap());
    simpleJdbcCall.execute(inParams);
  }

  @Override
  public void delete(int _memberId) throws Exception {
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName(MemberDTO.DELETE_PROCEDURE);
    MapSqlParameterSource inParams = new MapSqlParameterSource(MemberDTO.Parameters.p_id.toString(),_memberId);
    simpleJdbcCall.execute(inParams);
  }

  @Override
  public MemberDTO getInfo(int _memberId) throws Exception {
    MemberDTO obj = null;
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName(MemberDTO.GET_INFO_PROCEDURE);
    MapSqlParameterSource inParams = new MapSqlParameterSource(MemberDTO.Parameters.p_id.toString(),_memberId);
    Map outParams = simpleJdbcCall.execute(inParams);
    if(outParams != null){
      obj = new MemberDTO();
      obj.setId(Integer.parseInt(outParams.get(MemberDTO.Parameters.p_id.toString()).toString()));
      obj.setUser_Name(outParams.get(MemberDTO.Parameters.p_user_name.toString()).toString());
      obj.setPassword(outParams.get(MemberDTO.Parameters.p_password.toString()).toString());
      obj.setGroup_Id(Integer.parseInt(outParams.get(MemberDTO.Parameters.p_group_id.toString()).toString()));
      obj.setInsert_Date((Date)outParams.get(MemberDTO.Parameters.p_insert_date.toString()));
      obj.setLanguage(outParams.get(MemberDTO.Parameters.p_language.toString()).toString());
      obj.setLast_Change_Password((Date)outParams.get(MemberDTO.Parameters.p_last_change_password.toString()));
      obj.setLogined_Fail_Count(Integer.parseInt(outParams.get(MemberDTO.Parameters.p_logined_fail_count.toString()).toString()));
      obj.setStatus(Integer.parseInt(outParams.get(MemberDTO.Parameters.p_status.toString()).toString()));
      obj.setUser_Id(Integer.parseInt(outParams.get(MemberDTO.Parameters.p_user_id.toString()).toString()));
      obj.setLastLogined((Date)outParams.get(MemberDTO.Parameters.p_last_logined.toString()));
    }
    
    return obj;
  }

  @Override
  public MemberDTO getInfo(String _userName, String _password) throws Exception {
    MemberDTO obj = null;
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName(MemberDTO.GET_INFO_BY_UID_PROCEDURE);
    MemberDTO inMember = new MemberDTO();
    inMember.setUser_Name(_userName);
    inMember.setPassword(_password);
    MapSqlParameterSource inParams = new MapSqlParameterSource(inMember.getMap());
    Map outParams = simpleJdbcCall.execute(inParams);
//    System.out.println("outParams=" + outParams);
    if(outParams != null){
      obj = new MemberDTO();
      obj.setId(Integer.parseInt(outParams.get(MemberDTO.Parameters.p_id.toString()).toString()));
      obj.setUser_Name(_userName);
      obj.setPassword(_password);
      obj.setGroup_Id(Integer.parseInt(outParams.get(MemberDTO.Parameters.p_group_id.toString()).toString()));
      obj.setInsert_Date((Date)outParams.get(MemberDTO.Parameters.p_insert_date.toString()));
      obj.setLanguage(outParams.get(MemberDTO.Parameters.p_language.toString()).toString());
      obj.setLast_Change_Password((Date)outParams.get(MemberDTO.Parameters.p_last_change_password.toString()));
      obj.setLogined_Fail_Count(Integer.parseInt(outParams.get(MemberDTO.Parameters.p_logined_fail_count.toString()).toString()));
      obj.setStatus(Integer.parseInt(outParams.get(MemberDTO.Parameters.p_status.toString()).toString()));
      obj.setUser_Id(Integer.parseInt(outParams.get(MemberDTO.Parameters.p_user_id.toString()).toString()));
      obj.setLastLogined((Date)outParams.get(MemberDTO.Parameters.p_last_logined.toString()));
    }

    return obj;
  }

  @Override
  public List<MemberDTO> getAll() throws Exception {
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withFunctionName(MemberDTO.GET_ALL_PROCEDURE).returningResultSet(ConstantsDAO.RESULT_SET, ParameterizedBeanPropertyRowMapper.newInstance(MemberDTO.class));
    Map inParams = new HashMap();
    Map outMap = simpleJdbcCall.execute(new HashMap<String,Object>(0));

    return (List)outMap.get(ConstantsDAO.RESULT_SET);
  }
}
